package com.youxin.cpcenter.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.kafka.common.security.auth.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.BaseRequestBean;
import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.ent.RequestSwitchBean;
import com.youxin.busfoundation.bean.ent.ResponseAccountBean;
import com.youxin.busfoundation.bean.login.PayloadBean;
import com.youxin.busfoundation.bean.login.RequestLoginBean;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.bean.login.RequestRefreshToken;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.GlobalConfig;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.common.redis.RedisOperator;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.service.LoginService;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;


/**
 * 登录接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class LoginController extends BaseController{


	@Autowired
	private LoginService loginService;
	
	@LogMethod(id="ent.login.auth",eventContent={})
	@RequestMapping(value = "/api/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject login(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLoginBean bean) {
		try {
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ResponseObject responseObject= loginService.login(bean,httpServletResponse);
//			if(responseObject.success())
//			{
//				//responseCookie(responseObject.getData().toString(),httpServletResponse);
//				//responseObject.setData(null);
//				return responseObject;
//			}
			return responseObject;
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.token.refresh",eventContent={})
	@RequestMapping(value = "/api/token/refresh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject refreshToken(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestRefreshToken bean) {
		try {
			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			String token=LoginAuth.getToken(request);
			ObjectUtils.requestNonNull(token, "参数token不能为空");
			//UserInfo userInfo=LoginAuth.getUserInfo();
			bean.setToken(token);
			ResponseObject responseObject=loginService.refreshToken(bean,httpServletResponse);
//			if(responseObject.success())
//			{
//				responseCookie(responseObject.getData().toString(),httpServletResponse);
////				Cookie cookie=new Cookie(GlobalConfig.TOKEN_NAME, responseObject.getData().toString());
////				cookie.setMaxAge(GlobalConfig.DEFAULT_REFRESH_SECOND*2); 
////				httpServletResponse.addCookie(cookie);	
//				//responseObject.setData(null);
//				return responseObject;
//			}
			return responseObject;
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
//	private void responseCookie(String token,HttpServletResponse httpServletResponse)
//	{
//		Cookie cookie=new Cookie(GlobalConfig.TOKEN_NAME, token);
//		cookie.setMaxAge(new Long(GlobalConfig.DEFAULT_REFRESH_SECOND).intValue()*2); 
//		cookie.setPath("/");
//		httpServletResponse.addCookie(cookie);	
//	}

	@LogMethod(id="ent.login.verifycode",eventContent={})
	@RequestMapping(value = "/api/login/verifycode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject getVerifyCode(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLoginVerifyCode bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			
			if(!RegexUtils.isMobile(bean.getMobile()))
			{
				return ResponseObject.userFailure(BusinessMessages.NOT_VALID_MOBILE);
			}
			return loginService.requestLoginVerifyCode(bean);
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.account.switch",eventContent={})
	@RequestMapping(value = "/api/account/switch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject switchAccount(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestSwitchBean bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			return loginService.switchAccount(bean);
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@LogMethod(id="ent.account.list",eventContent={})
	@RequestMapping(value = "/api/account/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject listAccount(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody BaseRequestBean bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");			
			List<ResponseAccountBean> accounts=loginService.listAccount();
			return new ResponseObject(accounts);
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id="ent.login.logout",eventContent={})
	@RequestMapping(value = "/api/logout", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject logout(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestRefreshToken bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			String token=LoginAuth.getToken(request);
			bean.setToken(token);
			return loginService.logout(bean);
		} catch (Exception e) {
			logger.error("/login异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	
	@RequestMapping(value = "/tokentest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject testTokenInfo(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestRefreshToken bean) {
		try {			
			ObjectUtils.requestNonNull(bean, "参数不能为空");
			ObjectUtils.requestNonNull(bean.getToken(), "参数不能为空");
			String token=bean.getToken();
			PayloadBean payloadBean=LoginAuth.auth(token);
			long userid=payloadBean.getUserid();
			UserInfo userInfo=RedisOperator.getUserInfo(userid);			
			return new ResponseObject(userInfo);
		} catch (Exception e) {
			logger.error("/tokentest异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@RequestMapping(value = "/login/info", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject test(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws Exception {
		try {
			return loginService.loginfo();
		} catch (Exception e) {
			logger.error("/login/info",e);
			return ResponseObject.systemError(e);
		}

	}

	

}
